package org.ocean.online.web.workspace;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.markup.MarkupType;
import org.ocean.online.wicket.BaseWebPage;

public class ViewOnlyPage extends BaseWebPage{

	private static final long serialVersionUID = -4203360352478612203L;

	public ViewOnlyPage(){
		
	}
	
	@Override
	public MarkupType getMarkupType() {
		MarkupType type = new MarkupType(".*","text");
		return type;
	}

	@Override
	public void renderPage() {
		String path =this.getPageParameters().get("path").toString();
		if(StringUtils.isEmpty(path)){
			return;
		}
		try {
			File file = new File(path);
			if(!file.exists()){
				//render 404 page
			}
			FileInputStream fis = FileUtils.openInputStream(file);
			
			int size=0;
			while((size = fis.available())>0){
				byte[] buffer = new byte[size];
				fis.read(buffer);
				this.getResponse().getOutputStream().write(buffer);	
			}
			
		} catch (IOException e) {
			e.printStackTrace();
			// render 500 page
		}
	}
}
